//Given an integer array nums, return the maximum difference between two success
//ive elements in its sorted form. If the array contains less than two elements, r
//eturn 0. 
//
// You must write an algorithm that runs in linear time and uses linear extra sp
//ace. 
//
// 
// Example 1: 
//
// 
//Input: nums = [3,6,9,1]
//Output: 3
//Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) 
//has the maximum difference 3.
// 
//
// Example 2: 
//
// 
//Input: nums = [10]
//Output: 0
//Explanation: The array contains less than 2 elements, therefore return 0.
// 
//
// 
// Constraints: 
//
// 
// 1 <= nums.length <= 105 
// 0 <= nums[i] <= 109 
// 
// Related Topics 数组 桶排序 基数排序 排序 
// 👍 389 👎 0

package leetcode.editor.cn;

import org.junit.Assert;

import java.util.*;

class P164MaximumGap {
    public static void main(String[] args) {
        Solution solution = new P164MaximumGap().new Solution();
        Assert.assertEquals(solution.maximumGap(new int[]{15252,16764,27963,7817,26155,20757,3478,22602,20404,6739,16790,10588,16521,6644,20880,15632,27078,25463,20124,15728,30042,16604,17223,4388,23646,32683,23688,12439,30630,3895,
                7926,22101,32406,21540,31799,3768,26679,21799,23740}), 0);

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maximumGap(int[] nums) {
            if (nums.length == 1) {
                return 0;
            }
            int max = 0;
            Arrays.sort(nums);

            for (int i = 1; i < nums.length; i++) {
                int maxCom = nums[i] - nums[i - 1];
                if (maxCom > max) {
                    max = maxCom;
                }
            }
            return max;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}